<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 接受SF接口API
 * @author zxj
 * @description
 */

class Salesforce extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }


    /**
     * 接口一
     * type:接口类型:报价 TomRepair to oms
     * explain:同意OR拒绝报价
     */
    function tp_to_oms001()
    {
        $gpData = gp();
        if (!$gpData) {
            json_output($gpData, 'request failed', false);
        }
        $logInfo = array('file_path'=>__FILE__,'fun'=>__FUNCTION__,'data'=>$gpData);
        log_output($logInfo);
        $users = $this->common_model->selectFirst('user',
            array('account' => $gpData['user'], 'password' => md5($gpData['key']))
        );
        if (!$users) {
            json_output($gpData, 'request failed, invalid user', false);
        }
        //#产品信息
        $productArr = json_decode($gpData['products'], true);
        if (!$productArr || !is_array($productArr)) {
            json_output($productArr, 'The data format is not correct', false);
        }
        //#订单信息
        $oid = $gpData['oid'];
        $where = is_numeric($oid)?array('id' => $oid):array('order_num'=>$oid);
        $orders = $this->common_model->selectFirst('mt_order', $where);
        $customerNum = $orders['merchant_num']; //#商家客户
        if (!$orders) {
            json_output('', 'order not exist', false);
        }
        //#是否补充报价
        $isSupplement = $gpData['isSupplement'];
        //#修改状态
        foreach ($productArr as $key => $val) {
            $id = intval($key);
            $val = isset($val) ? intval($val) : 'Null';

            $products = $this->common_model->selectFirst('mt_product' , array('id' => $id , 'oid' => $orders['id']));
            if (!$products)// || in_array($products['acceptQuotation'],array(0,1)) 这一段代码170525添加,有报价状态的订单不允许在同意报价一次
                continue;

            $inData = $isSupplement ? array('supplementAcceptQuotation' => $val) : array('acceptQuotation' => $val);
            $this->common_model->update('mt_product', $inData, array('id' => $id));
            //环球易购补充报价
            if($customerNum==428 && $isSupplement && $val){
                $this->load->library('GlobalEGrow');
                $globalEGrow = new GlobalEGrow();
                $globalEGrow->transferPayInfo($id,6);
                $globalEGrow->transferAccessories($id);
            }
            //#品牌扣款
            if($products['brandSubTotal']>0 && is_numeric($val)){
                if($val===1){
                    if($isSupplement){
                        $payMoney = $products['supplementAmount'] - $products['discountSupplementAmount'];
                    }else{
                        $payMoney = $products['brandSubTotal'] - $products['discountSubAmountBrand'];
                    }
                }elseif($val===0){
                    $payMoney = $products['NAQAmount'] - $products['discountNAQAmount'];
                }
                $this->common_model->cutPayment($orders['id'],$products['brandCustomerNum'],$payMoney);
            }
            //#更新状态
            if ($products['status_id'] == 5) {
                $statusParam = array('uid'=>$gpData['uid'],'add_user'=>$gpData['add_user']);
                $this->common_model->resetStatus($id,$statusParam);
            }
        }

        //#更新订单金额
        $this->common_model->setOrderPrice($orders['id']);
        //#商家扣款
        if($customerNum){
            $payData = $this->common_model->cutPayment($orders['id'],$customerNum,'',$isSupplement);
        }
        if($payData){
            json_output($payData, $payData['message'], $payData['status']);
        }else{
            json_output('', 'successfully.', true);
        }

    }



    /**
     * 接口五
     * type:接口类型:报价 SF to oms
     * explain:同意OR拒绝报价，保存OR提交
     */
    function sf_to_oms005()
    {
        //$gpData = gp();
        $gpData = @file_get_contents('php://input');
        if (!$gpData) {
            json_output($gpData, '未接受到数据', false);
        }
        log_output($gpData);
        $gpData = json_decode($gpData, true);
        if ($gpData['token'] != 'A367FB69926E0CB863968A4E14011877') {
            json_output('', 'token error', false);
        }
        //保存OR提交
        $isSubmit = $gpData['isSubmit'];
        //是否补充报价
        $isSupplement = $gpData['object'] == 'RepairOrderDetail__c' ? 0 : 1;
        $productsList = $gpData['OMSIDS__c'];
        $orderNum = $gpData['OMSOrderID__c'];
        if (!$productsList || !is_array($productsList)) {
            json_output('', 'error:product data format', false);
        }
        //#订单信息
        $orders = $this->common_model->selectFirst('mt_order', array('order_num' => $orderNum));
        $customerNum = $orders['merchant_num']; //#商家客户
        if (!$orders) {
            json_output('', 'order not exist', false);
        }
        //#重置同意拒绝报价
        $this->common_model->update('mt_product', array('acceptQuotation' => 'Null'), array('oid' => $orders['id']));
        foreach ($productsList as $key => $val) {
            if (!isset($val)) continue;
            $id = intval($key);
            $val = $val == 'Yes' ? 1 : 0;
            //验证产品是否存在
            $products = $this->common_model->selectFirst('mt_product', array('id' => $id, 'oid' => $orders['id']));
            if (!$products) continue;
            $inData = $isSupplement ? array('supplementAcceptQuotation' => $val) : array('acceptQuotation' => $val);
            $this->common_model->update('mt_product', $inData, array('id' => $id));
            //#提交 -> 更新状态
            if ($isSubmit) {
                $statusParam = array('uid'=>$gpData['uid'],'add_user'=>$gpData['add_user']);
                $this->common_model->resetStatus($key,$statusParam);
                //#品牌扣款
                if($products['brandSubTotal']>0 && is_numeric($val)){
                    if($val===1){
                        if($isSupplement){
                            $payMoney = $products['supplementAmount'] - $products['discountSupplementAmount'];
                        }else{
                            $payMoney = $products['brandSubTotal'] - $products['discountSubAmountBrand'];
                        }
                    }elseif($val===0){
                        $payMoney = $products['NAQAmount'] - $products['discountNAQAmount'];
                    }
                    $this->common_model->cutPayment($orders['id'],$products['brandCustomerNum'],$payMoney);
                }
            }
        }
        //#更新订单支付金额
        $this->common_model->setOrderPrice($orders['id']);
        if($isSubmit){
            //#商家扣款
            $payData = $this->common_model->cutPayment($orders['id'],$customerNum,'',$isSupplement);
            json_output($payData, $payData['message'], $payData['status']);
        }
        json_output($gpData, '返回成功!', true);
    }


}
